package com.ulic.customerManApplication.mailmodule.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseDao;
import com.ulic.customerManApplication.commonmodule.bo.BirthdayMessage;
import com.ulic.customerManApplication.commonmodule.bo.MailSignature;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.mailmodule.bo.MailRecord;
import com.ulic.customerManApplication.mailmodule.dao.MailDao;
import com.ulic.customerManApplication.mailmodule.vo.MailRecordVO;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
@Component("mailDao")
public class MailDaoImpl extends BaseDao implements MailDao {

	Session session = null;
	@Override
	public void deleteMailRecord(MailRecord mailRecord) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.delete(mailRecord);
		session.flush();
	}

	@SuppressWarnings("unchecked")
	@Override
	public MailRecord getMailRecordById(int id) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(MailRecord.class);
		List<MailRecord> list = cri.add(Restrictions.eq("mail_id", id)).list();
		MailRecord mailRecord = null;
		if(list.size()>0){
			mailRecord	= (MailRecord)list.get(0);
		}
		return mailRecord;
	}

	@Override
	public PagerModel getMailRecords(int curPage) {
		String hql = "from MailRecord mailRecord order by sendTime desc";
		return searchPaginated(hql, curPage, CONSTANT.PageSize);
	}

	@Override
	public PagerModel getMailRecords(MailRecordVO mailRecordVO, int curPage) {
		StringBuilder sb = new StringBuilder();
		sb.append(" from MailRecord mailRecord where mailRecord.sender.um_user_id ="+mailRecordVO.getUser().getUm_user_id());
		if(mailRecordVO != null){
			if(mailRecordVO.getStartDate() != null && mailRecordVO.getStartDate().length() != 0){
				sb.append(" and to_date(mailRecord.sendTime,'YYYY-MM-DD  HH24:MI:SS') >= to_date('"+mailRecordVO.getStartDate()+" 00:00:00"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
			if(mailRecordVO.getEndDate() != null && mailRecordVO.getEndDate().length() != 0){
				sb.append(" and to_date(mailRecord.sendTime,'YYYY-MM-DD  HH24:MI:SS') <= to_date('"+mailRecordVO.getEndDate()+" 23:59:59"+"','YYYY-MM-DD  HH24:MI:SS') ");
			}
		}
		sb.append(" order by sendTime desc");
		return searchPaginated(sb.toString(), curPage, CONSTANT.PageSize);
	}

	@Override
	public void insertBirthdayBless(BirthdayMessage birthdayMessage) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(birthdayMessage);
	}

	@Override
	public void insertMailRecords(MailRecord mailRecord) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.save(mailRecord);
	}

	@SuppressWarnings("unchecked")
	@Override
	public BirthdayMessage getBirthdayMessageByType(User user,String type) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(BirthdayMessage.class);
		List<BirthdayMessage> list = cri.add(Restrictions.eq("user.um_user_id", user.getUm_user_id())).add(Restrictions.eq("type", type)).addOrder(Order.desc("id")).list();
		BirthdayMessage birthdayMessage = null;
		if(list.size()>0){
			birthdayMessage	= (BirthdayMessage)list.get(0);
		}
		return birthdayMessage;
	}

	/**
	 * Description: 获得邮件签名
	 * @Version1.0 
	 * 2012-9-10 下午05:33:15 李求智 (liqz@huilan.com) created
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public MailSignature getMailSignature(User user) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Criteria cri = session.createCriteria(MailSignature.class);
		List<MailSignature> list = cri.add(Restrictions.eq("user.um_user_id", user.getUm_user_id())).addOrder(Order.desc("oid")).list();
		MailSignature mailSignature = null;
		if(list.size()>0){
			mailSignature	= (MailSignature)list.get(0);
		}
		return mailSignature;
	}

	@Override
	public void saveMailSignature(MailSignature mailSignature) {
		session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		session.saveOrUpdate(mailSignature);
	}
}
